package fason
{
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	[Bindable]
	public class Game extends EventDispatcher
	{
		private static var LBL_LEN:int = 4;
		public var imageNumber:String;
		public var releaseNumber:String;
		public var label:String;
		public var title:String;
		public var saveType:String;
		public var romSize:String;
		public var publisher:String;
		public var location:int;
		public var sourceRom:String;
		public var language:int;
		public var files:ArrayCollection;
		public var im1CRC:String;
		public var im2CRC:String;
		public var comment:String;
		public var duplicateID:String;
		private static var IMSTEP:int = 500;
		public function Game()
		{
		}
		public function initUseXML(gameXML:XML):void
		{
			imageNumber = gameXML.imageNumber;
			releaseNumber = gameXML.releaseNumber;
			label = getID();
			title = gameXML.title;
			saveType = gameXML.saveType;
			romSize = gameXML.romSize;
			publisher = gameXML.publisher;
			location = gameXML.location;
			sourceRom = gameXML.sourceRom;
			language = gameXML.language;
			files = new ArrayCollection();
			var filesXMLList:XMLList = gameXML.files;
			for(var i:int=0;i<filesXMLList.length();i++){
				var loRomCRC:GameRomCRC = new GameRomCRC();
				loRomCRC.extension = filesXMLList[i].romCRC.@extension;
				loRomCRC.romCRC = filesXMLList[i].romCRC;
				files.addItem(loRomCRC);
			}
			im1CRC = gameXML.im1CRC;
			im2CRC = gameXML.im2CRC;
			comment = gameXML.comment;
			duplicateID = gameXML.duplicateID;
		}
		private function getID():String{
			if(releaseNumber.length >=LBL_LEN){
				return releaseNumber;
			}
			var tmpStr:String;
			tmpStr = "0000"+releaseNumber;
			return tmpStr.substr(tmpStr.length-4,4);
		}
		public function getImPath():ArrayCollection{
			var imPaths:ArrayCollection = new ArrayCollection();
			var step:int = int(imageNumber) / IMSTEP;
			var folder:String = (step*IMSTEP+1)+"-"+(step*IMSTEP+IMSTEP);
			imPaths.addItem(folder+"/"+imageNumber+"a.png");
			imPaths.addItem(folder+"/"+imageNumber+"b.png");
			return imPaths;
		}
		public function getLangFromAry(lanAry:ArrayCollection):String{
			for(var i:int=0;i<lanAry.length;i++){
				var loLang:GameLanguage = lanAry.getItemAt(i) as GameLanguage;
				if(language == loLang.id){
					return loLang.lang;
				}
			}
			return String(language);
		}
		public function getLanguageStr():String
		{
			switch(language)
			{
				case "1":
            		return "French";
				case "2":
            		return "English (US)";
				case "4":
            		return "Chinese";
				case "8":
            		return "Danish";
				case "16":
            		return "Dutch";
				case "32":
            		return "Finland";
				case "64":
            		return "German";
				case "128":
            		return "Italian";
				case "256":
            		return "Japanese";
				case "512":
            		return "Norwegian";
				case "1024":
            		return "Polish";
				case "2048":
            		return "Portuguese";
				case "4096":
            		return "Spanish";
				case "8192":
            		return "Swedish";
				case "16384":
            		return "English (UK) (only if English (US) already exist)";
				case "32768":
            		return "Portuguese (BR)";
				case "65536":
            		return "Korean";
            	default:
            		return String(language);
			}
		}
		public function getLocFromAry(locAry:ArrayCollection):String{
			for(var i:int=0;i<locAry.length;i++){
				var loLoc:GameLocation = locAry.getItemAt(i) as GameLocation;
				if(location == loLoc.id){
					return loLoc.location;
				}
			}
			return String(language);
		}
		public function getLocationStr():String
		{
			switch(location)
			{
				case "0":
            		return "Europe";
				case "1":
            		return "USA";
				case "2":
            		return "Germany";
				case "3":
            		return "China";
				case "4":
            		return "Spain";
				case "5":
            		return "France";
				case "6":
            		return "Italy";
				case "7":
            		return "Japan";
				case "8":
            		return "Netherlands";
				case "9":
            		return "England";
				case "10":
            		return "Denmark";
				case "11":
            		return "Finland";
				case "12":
            		return "Norway";
				case "13":
            		return "Poland";
				case "14":
            		return "Portugal";
				case "15":
            		return "Sweden";
				case "16":
            		return "Europe USA";
				case "17":
            		return "Europe USA Japan";
				case "18":
            		return "USA Japan";
				case "19":
            		return "Australia";
				case "20":
            		return "North Korea";
				case "21":
            		return "Brazil";
				case "22":
            		return "South Korea";
				case "23":
            		return "Europe Brazil";
				case "24":
            		return "Europe USA Brazil";
				case "25":
            		return "USA Brazil";
            	default:
            		return String(location);
			}
		}
	}
}